package com.ag.oa.core.service.impl;

import com.ag.oa.core.dao.ISignDao;
import com.ag.oa.core.dao.IUserDao;
import com.ag.oa.core.entity.vo.SignVO;
import com.ag.oa.core.entity.vo.UserVO;
import com.ag.oa.core.service.ISignService;
import com.ag.oa.core.service.UserService;
import com.ag.oa.enums.ScoreSourceTypeEnum;
import com.ag.oa.util.CustomUtil;
import com.ag.oa.util.DateUtils;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("signServiceImpl")
public class SignServiceImpl implements ISignService {

    @Autowired
    private ISignDao signDao;

    @Autowired
    private IUserDao userDao;

    @Autowired
    private UserService userService;

    private static final Logger logger = LoggerFactory.getLogger(SignServiceImpl.class);

    @Override
    public List<SignVO> getForSignPageListData(Integer page, Integer pageSize) {

        if (page==null) {
            page = 0;
        }
        if (pageSize==null) {
            pageSize = 18;
        }
        return signDao.getForPageList(page * pageSize, pageSize);
    }

    @Override
    public int saveUserNewWorkSign(Integer userId, String signDate, Integer year, Integer month, Integer day) {

        int res = 201;
        int count = signDao.getUserSignCountByDate(userId, signDate);
        if (count==0) {
            // 打卡成功, 用户积分 +5
            Integer transactionScore = 5;
            String nowTime = DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"); // 统一记录的时间
            Integer inSourceType = ScoreSourceTypeEnum.WORK_SIGN.getSourceType();
            String inAdditionalJson = "{}";
            String inDescription = "打卡";
            boolean inFlag = userService.doUserInScore(userId, transactionScore, nowTime, userId, inSourceType, inAdditionalJson, inDescription);
            if(inFlag){
                // 用户签到记录
                signDao.saveWorkSign(userId, signDate, year, month, day, nowTime);

                // 保存签到得分记录
                userDao.saveNewScoreRecord(null, userId, userId, transactionScore, inDescription, 1, nowTime);

                res = 200;
            }
        }
        return res;
    }

    @Override
    public List<SignVO> getUserSignedDateList(Integer userId, Integer page, Integer pageSize) {

        if (page==null) {
            page = 0;
        }
        if (pageSize==null) {
            pageSize = 16;
        }
        return signDao.getSignedDateListByUserId(userId, page * pageSize, pageSize);
    }

    @Override
    public List<SignVO> getUserSignedCalDateList(Integer userId, Integer year, Integer month) {

        return signDao.getSignedCalDateList(userId, year, month);
    }

    @Override
    public void getUserHasSignedInfo(Integer userId, JSONObject res) {

        logger.info("--签到检查: "+ userId);
        UserVO userVO = userDao.getById(userId);
        res.put("userInfo", userVO);
        if (userVO==null) {
            res.put("canISign", false);
            return;
        }

        String dateStr = CustomUtil.format(Calendar.getInstance().getTime(), "yyyy-MM-dd");
        int count = signDao.getUserSignCountByDate(userId, dateStr);
        res.put("canISign", count==0);
        logger.info("--是否可签到, userId: "+ userId +", dateStr: "+ dateStr +", count: "+ count);
    }

    @Override
    public int getUsersSignedTotalCountByDate(String signDate) {

        return signDao.getUserSignCountByDate(null, signDate);
    }

}
